package com.dawn.module.mp.controller.admin.msgtemplate;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import static com.dawn.framework.common.pojo.CommonResult.success;

import com.dawn.framework.excel.core.util.ExcelUtils;

import com.dawn.framework.apilog.core.annotation.ApiAccessLog;
import static com.dawn.framework.apilog.core.enums.OperateTypeEnum.*;

import com.dawn.module.mp.controller.admin.msgtemplate.vo.*;
import com.dawn.module.mp.dal.dataobject.msgtemplate.MsgTemplateDO;
import com.dawn.module.mp.service.msgtemplate.MsgTemplateService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 模板消息")
@RestController
@RequestMapping("/mp/msg-template")
@Validated
public class MsgTemplateController {

    @Resource
    private MsgTemplateService msgTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建模板消息")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:create')")
    public CommonResult<Long> createMsgTemplate(@Valid @RequestBody MsgTemplateSaveReqVO createReqVO) {
        return success(msgTemplateService.createMsgTemplate(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新模板消息")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:update')")
    public CommonResult<Boolean> updateMsgTemplate(@Valid @RequestBody MsgTemplateSaveReqVO updateReqVO) {
        msgTemplateService.updateMsgTemplate(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除模板消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mp:msg-template:delete')")
    public CommonResult<Boolean> deleteMsgTemplate(@RequestParam("id") Long id) {
        msgTemplateService.deleteMsgTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模板消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:query')")
    public CommonResult<MsgTemplateRespVO> getMsgTemplate(@RequestParam("id") Long id) {
        MsgTemplateDO msgTemplate = msgTemplateService.getMsgTemplate(id);
        return success(BeanUtils.toBean(msgTemplate, MsgTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模板消息分页")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:query')")
    public CommonResult<PageResult<MsgTemplateRespVO>> getMsgTemplatePage(@Valid MsgTemplatePageReqVO pageReqVO) {
        PageResult<MsgTemplateDO> pageResult = msgTemplateService.getMsgTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MsgTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模板消息 Excel")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMsgTemplateExcel(@Valid MsgTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MsgTemplateDO> list = msgTemplateService.getMsgTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模板消息.xls", "数据", MsgTemplateRespVO.class,
                        BeanUtils.toBean(list, MsgTemplateRespVO.class));
    }


    /**
     * 同步公众号模板
     */
    @PostMapping("/sync-template")
    @Operation(summary = "同步公众号模板")
    @PreAuthorize("@ss.hasPermission('mp:msg-template:create')")
    public CommonResult syncWxTemplate(@RequestParam("accountId") Long accountId)  {
        msgTemplateService.syncWxTemplate(accountId);
        return CommonResult.success(true);
    }

}
